import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;

import javax.swing.JPanel;

public class Player extends JPanel{

	private static final long serialVersionUID = 1L;
	int x;
	int y;
	int dy = 0;
	int ph = 1;
	
	boolean player = false;
	boolean fg = true;
	public Player(boolean fg){
		this.fg = fg;
	}
	
	public Player(int x, int y){
		this.x = x;
		this.y = y;
	}
	
	public void paintComponent(Graphics g){
		Graphics2D po = (Graphics2D)g;
		
		po.setColor(Color.white);
		
		po.fillRect(this.x, this.y, 20, 100);
		//po.drawLine(x + 10, y + 50, x + 1012, y + 50);
		
	}
	public void update(){
		if (y > Game.getHEIGHT() - 130){
			y=Game.getHEIGHT() - 130;
		}
		if (y < 0){
			y = 0;
		}
		y+= dy;
		collisionP();
	}
	
	
	public void collisionP(){

		
		if (Screen.b1.y + 18 >= y + 18 && Screen.b1.y + 18<= y + 113){
			if (Screen.b1.x + 18>= x&& Screen.b1.x + 18 <= x + 20){
				Screen.b1.dx = -Screen.b1.dx;
				
				try{
					if(dy < 0){
						ph = -Math.abs(ph);
						if(ph - 1 > -6){
							ph-=1;
						}
					}else if(dy > 0){
						ph = Math.abs(ph);
						if(ph + 1 < 6){
							ph+=1;
						}
					}
				}catch(Exception ex){}
				
				Screen.b1.dy = Screen.b1.dy + ph;
				
				if(Math.abs(Screen.b1.dx)+1 <2){
					Screen.b1.dx -= 1;
				}
				
			}
		}
	}
	public void updateC(){
		//wall collision
		if(fg)
		if (y > Game.getHEIGHT() - 130){
			y=Game.getHEIGHT() - 130;
		}
		if (y < 0){
			y = 0;
		}
		y+= dy;

		// AI
		if (Game.winner == 0){
			if(Screen.b1.y < y - 40){
				dy = -Math.abs(dy);
				if (dy > - 8){
					dy -=7;
				}
				System.out.println("YES UP" + dy);
			}else if (Screen.b1.y > y + (95 + 40)){
				dy = Math.abs(dy);
				if (dy < 8){
					dy +=7;
				}
				System.out.println("YES DOWN" + dy);
			} 
			
			
			if(Screen.b1.y <= y + 50 && Screen.b1.y >= y - 40){
				if (dy > -4){
				dy -= Math.abs(Screen.b1.dy);
				}
			}else if (Screen.b1.y >= y + (95 - 50) && Screen.b1.y <= y + (95 + 40)){
				if (dy < 4){
				dy += Math.abs(Screen.b1.dy);
				}
			} 
		
		}
		collisionC();
		
	}
	public void moveDown2(int a, int c){
		int b = a;
		dy = Math.abs(dy);
		if (dy + b< c){
			dy +=b;
		}
	}
	
	public void moveUp2(int a, int c){
		int b = a;
		dy = -Math.abs(dy);
		if (dy-b > - c){
			dy -=b;
		}
	}
	
	public void collisionC(){
		
		if (Screen.b1.y >= y && Screen.b1.y <= y + 95){
			if (Screen.b1.x >= x && Screen.b1.x <= x + 20){
				Screen.b1.dx = -Screen.b1.dx;
				try{
					if(dy < 0){
						ph = -Math.abs(ph);
						//ph -=1;
					}else if(dy > 0){
						ph = Math.abs(ph);
						//ph+=1;
					}
				}catch(Exception ex){}
				Screen.b1.dy = Screen.b1.dy + ph;
				if(Math.abs(Screen.b1.dx)+1 <13){
					Screen.b1.dx += 1;
				}
				
			}
		}
	}
	
	public void moveDown(){
		int b = 12;
		dy = Math.abs(dy);
		if (dy + b< 21){
			dy +=b;
			b+=8;
		}
	}
	
	public void moveUp(){
		int b = 12;
		dy = -Math.abs(dy);
		if (dy-b > - 21){
			dy -=b;
			b+=8;
		}
	}
	
	public void backNormal2(){
		while(Math.abs(dy) > 0){
		dy= Math.abs(dy) - 1;
		}
	}
	
}


